package fr.gmdev.mail.fonction.sms;

import java.net.URLEncoder;

import org.apache.commons.httpclient.HostConfiguration;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.GetMethod;

import fr.gmdev.mail.util.ApplicationParameters;


public class OvhSMSManager implements SMSManager {

	private String sendURL ;
	private String account ;
	private String login;
	private String password;
	private HttpClient client = new HttpClient();
	private HostConfiguration config = new HostConfiguration();
	
	public OvhSMSManager() {
		super();
		config.setHost("www.ovh.com",443,"https");
	}
	
	
	public void setAuthentification(String account, String login, String password){
		this.account = account;
		this.login = login;
		this.password = password;
	}
	
	public void sendMessage(String from,String to, String message) throws Exception {
		
		client.setHostConfiguration(config);
		GetMethod get = new GetMethod("/cgi-bin/sms/http2sms.cgi?account="+account+
				"&login="+login+
				"&password="+password+
				"&from="+from+
				"&to="+to+
				"&message=" + URLEncoder.encode(message));
//		HttpMethodParams params = new HttpMethodParams();
//	    params.setParameter("account", account);
//	    params.setParameter("login", login);
//	    params.setParameter("password", password);
//	    params.setParameter("from", from);
//	    params.setParameter("to", to);
//	    params.setParameter("message", message);
//		
//		get.setParams(params);
		
		get.addRequestHeader("accept", "application/json");


		int status = client.executeMethod(get);
		System.out.println(status);
		System.out.println(get.getResponseBodyAsString());
		System.out.println(get.getStatusCode() + " - " + get.getStatusText());
		if (status != 200){
			// on rejette le mail
			throw new Exception("Le serveur a renvoye une erreur " + status);
		}
	}
	
	public static void main(String[] args) throws Exception {
		SMSManager smsHndler = new OvhSMSManager();
		smsHndler.setAuthentification(
				ApplicationParameters.getSmsAccount(), 
				ApplicationParameters.getSmsLogin(), 
				ApplicationParameters.getSmsPassword());
 		smsHndler.sendMessage("0033660215004", "0033660215004", "C'est ok. Tu peux tester. Voies tes mails (message genere avec ma classe de test)");
//		smsHndler.sendMessage("0033684225978", "0033684225978", "ceci est un test");
	}
}
